/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaConfigMarcaDagua.java
 *
 * Created on 22/06/2011, 19:37:10
 */
package gestores.iteracao.manipuladores.marcadagua;

import gestores.janelas.JanelaConfigBase;
import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import model.ExtensionFilter;

public class JanelaConfigMarcaDagua extends JanelaConfigBase {

    private IDrawable marcadagua;
    private File imgfile;
    private Posicao posicao = Posicao.MEIOCENTRO;
    private Float scale = 1f;
    private Float alpha = 1f;

    /** Creates new form JanelaConfigMarcaDagua */
    public JanelaConfigMarcaDagua(ManipuladorMarcaDagua manipulador) {
        super(manipulador);
        initComponents();
        botoesRadio.add(topoE);
        botoesRadio.add(topoC);
        botoesRadio.add(topoD);
        botoesRadio.add(meioE);
        botoesRadio.add(meioC);
        botoesRadio.add(meioD);
        botoesRadio.add(baixoE);
        botoesRadio.add(baixoC);
        botoesRadio.add(baixoD);
        meioC.setSelected(true);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botoesRadio = new javax.swing.ButtonGroup();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tfCaminhoImagem = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        tfEscala = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tfAlpha = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        topoE = new javax.swing.JRadioButton();
        topoC = new javax.swing.JRadioButton();
        topoD = new javax.swing.JRadioButton();
        meioD = new javax.swing.JRadioButton();
        meioC = new javax.swing.JRadioButton();
        meioE = new javax.swing.JRadioButton();
        baixoE = new javax.swing.JRadioButton();
        baixoC = new javax.swing.JRadioButton();
        baixoD = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel1.setText("Imagem:");

        jButton3.setText("Procurar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        tfEscala.setText("1.0");
        tfEscala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfEscalaActionPerformed(evt);
            }
        });
        tfEscala.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfEscalaFocusLost(evt);
            }
        });

        jLabel2.setText("Escala:");

        jLabel3.setText("Opacidade:");

        tfAlpha.setText("1.0");
        tfAlpha.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfAlphaFocusLost(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jPanel1MouseClicked(evt);
            }
        });

        botoesRadio.add(topoE);
        topoE.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        topoE.setText("topoE");
        topoE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topoEActionPerformed(evt);
            }
        });

        botoesRadio.add(topoC);
        topoC.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        topoC.setText("topoC");
        topoC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topoCActionPerformed(evt);
            }
        });

        botoesRadio.add(topoD);
        topoD.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        topoD.setText("topoD");
        topoD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                topoDActionPerformed(evt);
            }
        });

        botoesRadio.add(meioD);
        meioD.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        meioD.setText("meioD");
        meioD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                meioDActionPerformed(evt);
            }
        });

        botoesRadio.add(meioC);
        meioC.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        meioC.setSelected(true);
        meioC.setText("meioC");
        meioC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                meioCActionPerformed(evt);
            }
        });

        botoesRadio.add(meioE);
        meioE.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        meioE.setText("meioE");
        meioE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                meioEActionPerformed(evt);
            }
        });

        botoesRadio.add(baixoE);
        baixoE.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        baixoE.setText("baixoE");
        baixoE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baixoEActionPerformed(evt);
            }
        });

        botoesRadio.add(baixoC);
        baixoC.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        baixoC.setText("baixoC");
        baixoC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baixoCActionPerformed(evt);
            }
        });

        botoesRadio.add(baixoD);
        baixoD.setFont(new java.awt.Font("Dialog", 1, 8)); // NOI18N
        baixoD.setText("baixoD");
        baixoD.setActionCommand("baixoD");
        baixoD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                baixoDActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(topoE)
                        .addGap(4, 4, 4)
                        .addComponent(topoC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(topoD))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(baixoE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(baixoC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(baixoD))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(meioE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(meioC)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(meioD)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(topoC)
                        .addComponent(topoD))
                    .addComponent(topoE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(meioE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(meioC)
                        .addComponent(meioD)))
                .addGap(4, 4, 4)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(baixoE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(baixoC)
                        .addComponent(baixoD)))
                .addContainerGap())
        );

        jLabel4.setText("Posição da Marca:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfEscala, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfAlpha, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 85, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton2)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(33, 33, 33)
                                .addComponent(jLabel4))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfCaminhoImagem, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tfCaminhoImagem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEscala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tfAlpha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        atualizarDados();
        salvarConfigs();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        JFileChooser fc = new JFileChooser();

        // restringe a amostra a diretorios apenas
        fc.setFileSelectionMode(JFileChooser.FILES_ONLY);

        int res = fc.showOpenDialog(null);

        if (res == JFileChooser.APPROVE_OPTION) {
            imgfile = new File(fc.getSelectedFile().getPath());
            atualizarDados();
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void topoCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topoCActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_topoCActionPerformed

    private void meioEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_meioEActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_meioEActionPerformed

    private void jPanel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jPanel1MouseClicked
    }//GEN-LAST:event_jPanel1MouseClicked

    private void tfEscalaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfEscalaFocusLost
        atualizaEscala();
    }//GEN-LAST:event_tfEscalaFocusLost

    private void tfAlphaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfAlphaFocusLost
        atualizaAlpha();
    }//GEN-LAST:event_tfAlphaFocusLost

    private void topoEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topoEActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_topoEActionPerformed

    private void topoDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_topoDActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_topoDActionPerformed

    private void meioCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_meioCActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_meioCActionPerformed

    private void meioDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_meioDActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_meioDActionPerformed

    private void baixoEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baixoEActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_baixoEActionPerformed

    private void baixoCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baixoCActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_baixoCActionPerformed

    private void baixoDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_baixoDActionPerformed
        atualizaPosicao();
    }//GEN-LAST:event_baixoDActionPerformed

    private void tfEscalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tfEscalaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tfEscalaActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton baixoC;
    private javax.swing.JRadioButton baixoD;
    private javax.swing.JRadioButton baixoE;
    private javax.swing.ButtonGroup botoesRadio;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton meioC;
    private javax.swing.JRadioButton meioD;
    private javax.swing.JRadioButton meioE;
    private javax.swing.JTextField tfAlpha;
    private javax.swing.JTextField tfCaminhoImagem;
    private javax.swing.JTextField tfEscala;
    private javax.swing.JRadioButton topoC;
    private javax.swing.JRadioButton topoD;
    private javax.swing.JRadioButton topoE;
    // End of variables declaration//GEN-END:variables

    private void salvarConfigs() {
        if (imgfile != null && imgfile.isFile() && imgfile.canRead() && new ExtensionFilter("jpg", "png", "bmp", "gif").accept(imgfile)) {
            marcadagua = new DrawableImage(imgfile, posicao, alpha, scale);
            if (marcadagua.valida()) {
                manipulador = new ManipuladorMarcaDagua(marcadagua);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Dados Inválidos");
            }
        } else {
            JOptionPane.showMessageDialog(this, "Dados Inválidos");
        }
    }

    private void atualizarDados() {
        if (imgfile != null) {
            this.tfCaminhoImagem.setText(imgfile.getPath());
        }
        atualizaPosicao();
        atualizaAlpha();
        atualizaEscala();
    }

    private void atualizaEscala() {
        try {
            if (!tfEscala.getText().contains(".")) {
                tfEscala.setText(tfEscala.getText() + ".0");
            }
            float tempescala = Float.parseFloat(tfEscala.getText());
            if (tempescala != Float.NaN && tempescala >= 0f && tempescala <= 1f) {
                this.scale = tempescala;
            } else {
                throw new Exception("Erro! valor inválido no campo Escala.");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }
    }

    private void atualizaAlpha() {
        try {
            if (!tfAlpha.getText().contains(".")) {
                tfAlpha.setText(tfAlpha.getText() + ".0");
            }
            float tempalpha = Float.parseFloat(tfAlpha.getText());
            if (tempalpha != Float.NaN && tempalpha >= 0f && tempalpha <= 1f) {
                this.alpha = tempalpha;
            } else {
                throw new Exception("Erro! valor inválido no campo Transparência.");
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, e.getMessage());
        }

    }

    private void atualizaPosicao() {
        Posicao p = Posicao.MEIOCENTRO;
        Component[] components = jPanel1.getComponents();
        String sel = "meioC";
        for (Component c : components) {
            if (c instanceof JRadioButton && ((JRadioButton) c).isSelected()) {
                sel = ((JRadioButton) c).getText();
            }
        }

        if (sel.equals("topoE")) {
            p = Posicao.TOPOESQUERDA;
        } else if (sel.equals("topoC")) {
            p = Posicao.TOPOCENTRO;
        } else if (sel.equals("topoD")) {
            p = Posicao.TOPODIREITA;
        } else if (sel.equals("meioE")) {
            p = Posicao.MEIOESQUERDA;
        } else if (sel.equals("meioC")) {
            p = Posicao.MEIOCENTRO;
        } else if (sel.equals("meioD")) {
            p = Posicao.MEIODIREITA;
        } else if (sel.equals("baixoE")) {
            p = Posicao.BAIXOESQUERDA;
        } else if (sel.equals("baixoC")) {
            p = Posicao.BAIXOCENTRO;
        } else if (sel.equals("baixoD")) {
            p = Posicao.BAIXODIREITA;
        }
        this.posicao = p;
    }
}
